<?php
// +----------------------------------------------------------------------
// | Iecms [ Make the most exquisite CMS ]
// +----------------------------------------------------------------------
// | Copyright (c) 2009~2019 http://www.iemoo.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: iemoo <i@iemoo.com>
// +----------------------------------------------------------------------
// | DateTime: 2019/11/19 11:08
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\common\model\AdminAuthrule as AdminAuthruleModel;

use app\admin\validate\AdminAuthrule as AdminAuthruleValidate;
use think\exception\ValidateException;


use think\facade\Request;
use think\facade\View;

/**
 * 后台菜单
 * Class Menu
 * @package app\admin\controller
 */
class Auth extends Base
{


    public function __construct()
    {
        parent::__construct();
        $admin_menu_list       = AdminAuthruleModel::order('sort desc,id asc')->select();
		$admin_menu_list = $admin_menu_list->toArray();

        $admin_menu_level_list = tree_cate($admin_menu_list);

		View::assign('admin_menu_level_list',$admin_menu_level_list);

    }

    /**
     * 后台菜单
     * @return mixed
     */
    public function index()
    {
        return View::fetch('index');
    }

    /**
     * 添加菜单
     * @param string $pid
     * @return mixed
     */
    public function add($id = '')
    {
		

		if (Request::isPost()) {

			$data	= Request::only(['pid', 'title', 'name', 'icon', 'status', 'sort', '__token__']);

			try {
				validate(AdminAuthruleValidate::class)->check($data);
			} catch (ValidateException $e) {
				// 验证失败 输出错误信息
				$this->error($e->getError());
			}

   
			if (AdminAuthruleModel::create($data)) {
				cache('auth_ids', NULL);
				$this->success('保存成功',url('admin/auth/index'));
			} else {
				$this->error('保存失败');
			}
      
        }

		View::assign('pid',$id);
		return View::fetch('add');


    }

    /**
     * 编辑菜单
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
      
		if (Request::isPost()) {

			$data	= Request::only(['id', 'pid', 'title', 'name', 'icon', 'status', 'sort', '__token__']);

			try {
				validate(AdminAuthruleValidate::class)->check($data);
			} catch (ValidateException $e) {
				// 验证失败 输出错误信息
				$this->error($e->getError());
			}

   
			if (AdminAuthruleModel::update($data,['id'=>$id])) {
				$this->success('更新成功');
			} else {
				$this->error('更新失败');
			}
      
        }

		$admin_auth = AdminAuthruleModel::find($id);
		
		View::assign('admin_auth',$admin_auth);
		return View::fetch('edit');


    }


    /**
     * 删除菜单
     * @param $id
     */
    public function delete($id)
    {
        $sub_menu = AdminAuthruleModel::where(['pid' => $id])->find();
        if (!empty($sub_menu)) {
            $this->error('此菜单下存在子菜单，不可删除');
        }
        if (AdminAuthruleModel::destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    /**
     * 快捷更新
     */
    public function setStatus(){
	
        if (Request::isGet()) {
			$data = Request::only(['id', 'auth_open', 'status', 'sort']);

            if (AdminAuthruleModel::update($data) !== false) {
                $this->success('更新成功');
            } else {
                $this->error('更新失败');
            }
        }
    }


}